import os
import time
from multiprocessing import Process


def run_exe(exe_name):
    os.system(exe_name)


def kill_exe(sleep_time, exe_name):
    time.sleep(sleep_time)
    exe_name = exe_name.split('/')[-1]
    exe_name = exe_name.split('"')[0]
    kill_cmd = 'killall ' + exe_name
    os.system(kill_cmd)


def init_app():
    home_path = os.path.expanduser('~')
    desk_path = os.path.join(home_path, '桌面')
    shotcut_name = []
    shotcut_path = []
    exec_cmd = []
    for _, _, file_name in os.walk(desk_path):
        pass

    for i in file_name:
        if i.split('.')[-1] == 'desktop':
            shotcut_name.append(i)

    for i in shotcut_name:
        if i == 'lance.desktop':
            shotcut_name.remove(i)

    for i in shotcut_name:
        shotcut_path.append(os.path.join(desk_path, i))

    for i in shotcut_path:
        f = open(i)
        for line in f.readlines():
            line = line.strip()
            header = line.split("=")[0]
            if header == "Exec":
                exec_cmd.append(line.split("Exec=")[-1])
                break

    for i in exec_cmd:
        p = Process(target=run_exe, args=(i,))
        t = Process(target=kill_exe, args=(20, i))
        p.start()
        t.start()
        p.join()
        t.join()
